//
//  ZFBNoteController.m
//  支付宝
//
//  Created by GD on 16/8/6.
//  Copyright © 2016年 HM. All rights reserved.
//

#import "ZFBNoteController.h"
#import "ZFBNewNoteViewController.h"
#import "ZFBDetailNoteController.h"

@interface ZFBNoteController ()<ZFBNewNoteViewControllerDelegate,ZFBDetailNoteControllerDelegate>

@end

static NSString *cellID = @"myNote";

@implementation ZFBNoteController{
    NSMutableArray<NSDictionary *> *_listNote;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(newNote)];
    
    self.title = @"备忘录";
    
    [self loadData];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}

- (void)newNote {
    ZFBNewNoteViewController *newNote = [[ZFBNewNoteViewController alloc] init];
    newNote.delegate = self;
    [self.navigationController pushViewController:newNote animated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _listNote.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    // Configure the cell...
    NSDictionary *dict = _listNote[indexPath.row];
    cell.textLabel.text = [dict valueForKey:kZFBNoteContentKey];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ZFBDetailNoteController *detailNote = [[ZFBDetailNoteController alloc] init];
    detailNote.detailNoteDict = _listNote[indexPath.row];
    detailNote.delegate = self;
    [self.navigationController pushViewController:detailNote animated:YES];

}

// MARK : - ZFBDetailNoteControllerDelegate
-(void)detailNoteController:(ZFBDetailNoteController *)detailNoteController didFinishEditNote:(NSDictionary *)dict{
    //如何获取
//    NSString *noteDate = dict[kZFBNoteDateKey];
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
//    NSDictionary *selDict = _listNote[indexPath.row];
    
    [_listNote replaceObjectAtIndex:indexPath.row withObject:dict];
    
    [self.tableView reloadData];
}

#pragma mark  ZFBNewNoteViewControllerDelegate
-(void)newNoteController:(ZFBNewNoteViewController *)newNoteController didFinishAddNote:(NSDictionary *)dict{

    [_listNote addObject:dict];
    
    [self.tableView reloadData];
    
    [_listNote writeToFile:[self filePathWithFileName:@"myNotes.plist"] atomically:YES];

}

-(NSString *)filePathWithFileName:(NSString *)fileName{
    
    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filepath = [docPath stringByAppendingPathComponent:fileName];
    
    return filepath;
}

-(void)loadData{
    
    if (_listNote == nil) {
        _listNote = [NSArray arrayWithContentsOfFile:[self filePathWithFileName:@"myNotes.plist"]];
    }
    
    if (_listNote == nil) {
        
        _listNote = [NSMutableArray array];
    }
}
@end
